package com.xiangyouhui.base_project.component;

import com.alibaba.fastjson.JSONException;
import com.xiangyouhui.base_project.domain.DataResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LogManager.getLogger();

    @ExceptionHandler(value = JSONException.class)
    @ResponseStatus(HttpStatus.ACCEPTED)
    public DataResult jsonExceptionHandler(HttpServletRequest request, Exception e) {
        logger.warn(request.getRequestURI() + "\tPARAM_ERROR");

        return new DataResult(DataResult.PARAM_ERROR, null, null);
    }
}
